package org.hxyjs.service.impl;

import org.hxyjs.dao.ProgrammeDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Programme;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.ProgrammeService;
import org.hxyjs.vo.ProgrammeView;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service("programmeService")
public class ProgrammeServiceImpl implements ProgrammeService {
    @Resource
    ProgrammeDao programmeDao;

    //查询全部
    @Override
    public DTO<Programme> getAllProgrammes()  throws Exception{
        List<Programme> programmes = programmeDao.getAllProgrammes();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(programmes).isPresent()?MessageEnum.GETPROGRAMMEALLSUCCESS:MessageEnum.GETPROGRAMMEALLFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage(),programmes);
    }

    //删除
    @Override
    public DTO<Programme> deleteProgrammeByPrno(String prno)  throws Exception{
        Integer result = programmeDao.deleteProgrammeByPrno(prno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEPROGRAMMESUCCESS:MessageEnum.DELETEPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //按主键查询
    @Override
    public DTO<Programme> getProgrammeByPrno(String prno)  throws Exception{
        Programme programmes = programmeDao.getProgrammeByPrno(prno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(programmes).isPresent()?MessageEnum.GETPROGRAMMESUCCESS:MessageEnum.GETPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage(),programmes);
    }

    //修改
    @Override
    public DTO<Programme> updateProgrammeByPrno(ProgrammeView programmeView) throws Exception {
        Programme programme = dateFormat(programmeView);
        Integer result = programmeDao.updateProgrammeByPrno(programme);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEPROGRAMMESUCCESS:MessageEnum.UPDATEPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage());
    }

    //增加
    @Override
    public DTO<Programme> addProgramme(Programme programme) throws Exception {
        Integer result = programmeDao.addProgramme(programme);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDPROGRAMMESUCCESS:MessageEnum.ADDPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Programme> getProgrammeByPage(PageInfo pageInfo ,String cacheId) throws Exception {
        List<Programme> programmes = programmeDao.getProgrammeByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(programmes).isPresent()?MessageEnum.GETPROGRAMMESUCCESS:MessageEnum.GETPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage(),programmes,pageInfo);
    }

    public DTO<Programme> getProgrammeByPage(PageInfo pageInfo) throws Exception {
        List<Programme> programmes = programmeDao.getProgrammeByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(programmes).isPresent()?MessageEnum.GETPROGRAMMESUCCESS:MessageEnum.GETPROGRAMMEFAIL;
        return new DTO<Programme>(messageEnum.getCode(),messageEnum.getMessage(),programmes,pageInfo);
    }
    public Programme dateFormat(ProgrammeView programmeView) throws Exception{
        Programme programme = new Programme();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        programme.setPrdate(sdf.parse(programmeView.getPrdate()));
        programme.setErnu(programmeView.getErnu());
        programme.setPrno(programmeView.getPrno());
        programme.setEno(programmeView.getEno());
        programme.setPrbuynum(programmeView.getPrbuynum());
        programme.setPrcontent(programmeView.getPrcontent());
        programme.setPrlikes(programmeView.getPrlikes());
        programme.setPrprice(programmeView.getPrprice());
        programme.setPrtitle(programmeView.getPrtitle());


        return programme;

    }

}
